			/* Define Canvas */

			var canvas;
			var stage;
			
			var check = 0;

			/* Background */

			var bgSrc = new Image();
			var bg;

			/* Button */

			var btnSrc = new Image();
			var btn;
			
			var btnSrc2 = new Image();
			var btn2;
			
			var orange = new Image();
			
			var sectors = new Array();
			var rectangles = new Array();
			var colours = [[255, 255, 255], [150, 255, 255], [255, 150, 255], [255, 255, 150], [0, 0, 0]];
			var scores = [0, 1, 2, 3, 4, 0, 1, 2, 3];
			
			var previous = 0;
			var main = new Array();
			var gameOver = false;
			var showLevels = false;

			/* Variables */

			var centreX = 275;
			var centreY = 150;
			var gfxLoaded = 0; //Will serve as preloader

			/* Constructor */

			function Main()
			{
				/* Link Canvas */
				
				canvas = document.getElementById('HelloWorld');
				stage = new Stage(canvas);
				
				/* Enable Mouse Events */
					
				stage.mouseEventsEnabled = true;
				
					
				/* Load GFX */
					
				bgSrc.src = 'bg.png';
				bgSrc.name = 'bg';
				bgSrc.onload = loadGfx;
				
				/*btnSrc.src = 'button.png';
				btnSrc.name = 'button';
				btnSrc.onload = loadGfx;
				
				btnSrc2.src = 'button.png';
				btnSrc2.name = 'button2';
				btnSrc2.onload = loadGfx;
				
				orange.src = 'orange.png';
				orange.name = 'orange';
				orange.onload = loadGfx;*/
				
				/* Ticker */
				
				Ticker.setFPS(30);
				Ticker.addListener(stage);
			}

			function loadGfx(e)
			{
				if(e.target.name = 'bg'){bg = new Bitmap(bgSrc);}
				//if(e.target.name = 'button'){btn = new Bitmap(btnSrc);}
				//if(e.target.name = 'button2'){btn2 = new Bitmap(btnSrc2);}
				//if(e.target.name = 'orange'){btn2 = new Bitmap(orange);}
				
				gfxLoaded++;
				
				/* Display graphics until all of them are loaded */
				
				if(gfxLoaded == 1)
				{
					mainInterface();
				}
			}
			
			function mainInterface()
			{
				if (showLevels == true)
				{
					return;
				}
				scores = [0, 1, 2, 3, 4, 0, 1, 2, 3];
				gameOver = false;
				stage.removeAllChildren();
				stage.enableMouseOver();
				var single = new Text("Single Player", "20px Arial", "white");
				single.textAlign = "center";
				single.x = centreX;
				single.y = centreY+30;
				single.maxWidth=270;
				var multi = new Text("Multiplayer", "20px Arial", "white");
				multi.textAlign = "center";
				multi.x = centreX;
				multi.y = centreY-30;
				multi.maxWidth=150;
				var graphics = new Graphics();
				graphics.beginFill(Graphics.getRGB(0,0,0,1));
				graphics.drawRoundRect(275-70, 155, 150, 40, 20);
				singleShape = new Shape(graphics);
				var graphics = new Graphics();
				graphics.beginFill(Graphics.getRGB(0,0,0,1));
				graphics.drawRoundRect(275-70, 95, 150, 40, 20);
				multiShape = new Shape(graphics);
				multiShape.mouseEnable = true;
				multiShape.onMouseOut = dehighlight;
				multiShape.onMouseOver = highlight;
				singleShape.mouseEnable = true;
				singleShape.onMouseOut = dehighlight;
				singleShape.onMouseOver = highlight;
				singleShape.onClick = next;
				main[0] = multiShape;
				main[1] = singleShape;
				main[2] = single;
				main[3] = multi;
				stage.addChild(bg, singleShape, multiShape, single, multi);
				stage.update();
			}
			
			function levels()
			{
				showLevels = true;
				var graphics;
				var shape;
				stage.removeAllChildren();
				stage.addChild(bg);
				for(var level = 0; level < 8; level++)
				{
					graphics = new Graphics();
					graphics.beginFill(Graphics.getRGB(255,255,255,1));
					graphics.drawRoundRect(145 + 70*parseInt(level/2), 70 + 70*parseInt(level%2), 40, 40, 5);
					shape = new Shape(graphics);
					stage.addChild(shape);
					console.log("Test");
				}
				graphics = new Graphics();
				graphics.beginFill(Graphics.getRGB(0,0,0,1));
				graphics.drawRoundRect(195-70, 220, 110, 40, 20);
				shape = new Shape(graphics);
				shape.onClick = backMain;
				shape.onMouseOver = focusBack;
				stage.addChild(shape);
				var label = new Text("Back", "20px Arial", "white");
				label.textAlign = "center";
				label.x = 175;
				label.y = 245;
				label.maxWidth=150;
				stage.addChild(label);
				stage.update();
			}
			
			function backMain()
			{
				showLevels = false;
				mainInterface();
			}
			
			function level()
			{
			
			}
			
			function next()
			{
				console.log("TEST");
				stage.removeAllChildren();
				//gameInterface();
				levels();
			}
			
			function highlight(mouseEvent)
			{
				if (showLevels == true) 
				{
					return;
				}
				stage.removeAllChildren();
				stage.addChild(bg);
				if (mouseEvent.stageX >= 275 && mouseEvent.stageX <= 275 + 150) {
					if (mouseEvent.stageY >= 155 && mouseEvent.stageY <= 155+40) {
						var graphics = new Graphics();
						graphics.beginFill(Graphics.getRGB(255,255,255,1));
						graphics.drawRoundRect(275-71, 154, 152, 42, 20);
						singleFocusShape = new Shape(graphics);
						stage.addChild(singleFocusShape);
					}
				} 
				if (mouseEvent.stageX >= 275 && mouseEvent.stageX <= 275 + 150) {
					if (mouseEvent.stageY >= 95 && mouseEvent.stageY <= 95+40) {
						var graphics = new Graphics();
						graphics.beginFill(Graphics.getRGB(255,255,255,1));
						graphics.drawRoundRect(275-71, 94, 152, 42, 20);
						singleFocusShape = new Shape(graphics);
						stage.addChild(singleFocusShape);
					}
				}
				stage.addChild(main[0], main[1], main[2], main[3]);
				stage.update();
			}
			
			function dehighlight()
			{
				if (showLevels == true)
				{
					return;
				}
				stage.removeAllChildren();
				stage.addChild(bg);
				stage.addChild(main[0], main[1], main[2], main[3]);
				stage.update();
			}

			function gameInterface()
			{
				//btn.x = centerX - 40;
				//btn.y = centerY - 12;
				//btn2.x = centerX + 40;
				//btn2.y = centerY - 12;
				stage.enableMouseOver();
				stage.addChild(bg);
				
				var graphics = new Graphics();
				/*graphics.beginFill(Graphics.getRGB(255,255,255,1));
				graphics.drawRect(275, 150, 20, 20);
				shape = new Shape(graphics);
				var currentShape = shape;*/
				
				for (var graphic = 0; graphic < 9; graphic++) 
				{
					graphics.beginFill(Graphics.getRGB(colours[scores[graphic]][0],colours[scores[graphic]][1],colours[scores[graphic]][2],1));
					graphics.drawRoundRect(195 + 50*parseInt(graphic/3), 70 + 50*parseInt(graphic%3), 40, 40, 5);
					rectangles[graphic] = [195 + 50*parseInt(graphic/3), 70 + 50*parseInt(graphic%3), 40, 40];
					sectors[graphic] = new Shape(graphics);
					sectors[graphic].mouseEnabled = true;
					sectors[graphic].onPress = button;
					sectors[graphic].onMouseOver = focus;
					sectors[graphic].onMouseOut = defocus;
					stage.addChild(sectors[graphic]);
				}
				/*
				var graphics = new Graphics();
				graphics.beginFill(Graphics.getRGB(255,255,255,1));
				graphics.drawRect(275, 150, 20, 20);
				shape = new Shape(graphics);
				
				stage.addChild(bg, shape);*/
				stage.update(); // Very Important
				
				/* Add button listener */
			}
			
			function button(mouseEvent) {
				if (gameOver == true)
				{
					return;
				}
				//console.log(mouseEvent);
				for(var item = 0; item < 9; item++)
				{
					//console.log("X: " + (mouseEvent.stageX >= rectangles[item][0] && mouseEvent.stageX <= rectangles[item][0] + rectangles[item][2]).toString());
					if(mouseEvent.stageX >= rectangles[item][0] && mouseEvent.stageX <= rectangles[item][0] + rectangles[item][2])
					{
						//console.log(rectangles[item][1]);
						//console.log(rectangles[item][1] + rectangles[item][3]);
						//console.log("Y: " + (mouseEvent.stageY >= rectangles[item][1] && mouseEvent.stageY <= rectangles[item][1] + rectangles[item][3]).toString());
						if(mouseEvent.stageY >= rectangles[item][1] && mouseEvent.stageY <= rectangles[item][1] + rectangles[item][3]) 
						{
							//console.log("Success");
							if (engine(item) == true) {
								console.log("Tested the engine");
								colourRect(item);
							}
							//console.log(item);
						}
					}
				}
			}
			
			function focus(mouseEvent) {
				if (gameOver == true) 
				{
					return;
				}
				//console.log(mouseEvent);
				for(var item = 0; item < 9; item++)
				{
					//console.log("X: " + (mouseEvent.stageX >= rectangles[item][0] && mouseEvent.stageX <= rectangles[item][0] + rectangles[item][2]).toString());
					if(mouseEvent.stageX >= rectangles[item][0] && mouseEvent.stageX <= rectangles[item][0] + rectangles[item][2])
					{
						//console.log(rectangles[item][1]);
						//console.log(rectangles[item][1] + rectangles[item][3]);
						//console.log("Y: " + (mouseEvent.stageY >= rectangles[item][1] && mouseEvent.stageY <= rectangles[item][1] + rectangles[item][3]).toString());
						if(mouseEvent.stageY >= rectangles[item][1] && mouseEvent.stageY <= rectangles[item][1] + rectangles[item][3]) 
						{
							//console.log("Success");
							var graphics = new Graphics();
							graphics.beginFill(Graphics.getRGB(255,255,255,1));
							graphics.drawRoundRect(rectangles[item][0]-1, rectangles[item][1]-1, rectangles[item][2]+2, rectangles[item][3]+2, 5);
							var shape = new Shape(graphics);
							stage.removeAllChildren();
							stage.addChild(bg);
							stage.addChild(shape);
							for(var sector = 0; sector < 9; sector++) {
								stage.addChild(sectors[sector]);
							}
							stage.update();
							//colourRect(item);
							//console.log(item);
						}
					}
				}
			}
			
			function defocus(mouseEvent)
			{
				if (gameOver == true)
				{
					return;
				}
				stage.removeAllChildren();
				stage.addChild(bg);
				for(var sector = 0; sector < 9; sector++) {
					stage.addChild(sectors[sector]);
				}
				stage.update();
			}
			
			function colourRect(value)
			{
				//console.log(value);
				//console.log(scores[value]);
				//console.log(colours[scores[value]]);
				stage.removeAllChildren();
				//var graphics = new Graphics();
				//console.log(value);
				//console.log(scores[value]);
				//console.log(colours[scores[value]]);
				sectors[value].graphics.beginFill(Graphics.getRGB(colours[scores[value]][0],colours[scores[value]][1],colours[scores[value]][2],1));
				sectors[value].graphics.drawRoundRect(rectangles[value][0], rectangles[value][1], rectangles[value][2], rectangles[value][3], 5);
				//shape.graphics.endFill();
				//shape = new Shape(graphics);
				stage.addChild(bg);
				for (var item = 0; item < 9; item++) 
				{
					stage.addChild(sectors[item]);
				}
				stage.update();
			}
			
			function engine(value) {
				scores[value] = scores[value] + scores[previous];
				if (scores[value] > 4) 
				{
					scores[value] = scores[value] - 4;
				}
				console.log("Start");
				console.log(previous);
				console.log(value);
				if (scores[0] == scores[8]) {
					gameOver = true;
					var graphics = new Graphics()
					graphics.beginFill(Graphics.getRGB(0,0,0,1));
					graphics.drawRect(0, 0, 550, 300);
					click = new Shape(graphics);
					click.onClick = mainInterface;
					stage.removeAllChildren();
					var label = new Text("You Won, Congratulations", "40px Arial", "white");
					label.textAlign = "center";
					label.x = centreX;
					label.y = centreY;
					label.maxWidth=270;
					stage.addChild(click, bg, label);
					stage.update();
					console.log("Worked");
					return false;
				}
				check = checkMoves(value);
				previous = value;
				return check;
			}
			
			function checkMoves(value) {
				var allowedMoves = [[2, 4], [1, 3, 5], [2, 6], [1, 5, 7], [2, 4, 6, 8], [3, 5, 9], [4, 8], [5, 7, 9], [8, 6]];
				var check;
				for(var iter = 0; iter < allowedMoves[value].length;iter++) {
					check = allowedMoves[value][iter];
					console.log(check-1);
					console.log(previous == check-1);
					if (previous == check-1) {
						return true;
					}
				}
				return false;
			}